package com.ywm.common.generator;

import com.ywm.common.utils.StrUtils;
import org.apache.commons.lang.ObjectUtils;
import org.apache.velocity.VelocityContext;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GeneratorTable {
	
	/***
	 * 生成文件模板文件
	 */
	private static String GENERATOR_CONFIG_VM;
	
	/**
	 * 模板XML文件
	 */
	private static String GENERATOR_CONFIG_XML;

	/**
	 * 控制层地址
	 */
	private static String CONTROLLER_VM;
	
	/**
	 * 服务接口地址
	 */
	private static String SERVICE_VM;
	
	/**
	 * 服务接口实现地址
	 */
	private static String SERVICE_IMPL_VM;
	
	/**
	 * Dao接口地址
	 */
	private static String DAO_VM;
	
	/**
	 * Dao接口实现地址
	 */
	private static String DAO_IMPL_VM;
	
	/**
	 * 主键返回
	 */
	private static Map<String, String> LAST_INSERT_ID_TABLES = null;
	
	/**
	 * 默认包名
	 */
	private static final String DEFAULT_PACKAGE = "com.ywm";
	
	static {
		GENERATOR_CONFIG_VM = GeneratorTable.class.getResource("/vm/GeneratorConfig.vm").getPath().replaceFirst("/", "");
		GENERATOR_CONFIG_XML =  GeneratorTable.class.getResource("/").getPath().replace("/target/classes/", "") + "/src/main/resources/generatorConfig.xml";
		CONTROLLER_VM = GeneratorTable.class.getResource("/vm/Controller.vm").getPath().replaceFirst("/", "");
		SERVICE_VM = GeneratorTable.class.getResource("/vm/Service.vm").getPath().replaceFirst("/", "");
		SERVICE_IMPL_VM = GeneratorTable.class.getResource("/vm/ServiceImpl.vm").getPath().replaceFirst("/", "");
		DAO_VM = GeneratorTable.class.getResource("/vm/Dao.vm").getPath().replaceFirst("/", "");
		DAO_IMPL_VM = GeneratorTable.class.getResource("/vm/DaoImpl.vm").getPath().replaceFirst("/", "");
		LAST_INSERT_ID_TABLES = new HashMap<String, String>();
	}
	

	public void generatorTabel(String table, String module) {
		
	}
	
	public static void generatorTabel(Map<String, List<Map<String, String>>> tableData, String bashPath, String project) throws Exception {
		 for(Map.Entry<String, List<Map<String, String>>> entry : tableData.entrySet()) {
			 List<Map<String, String>> tableList = entry.getValue();
			VelocityContext context = new VelocityContext();
			context.put("tables", tableList);
			context.put("generator_javaModelGenerator_targetPackage", DEFAULT_PACKAGE+".model."+entry.getKey());
			context.put("generator_javaClientGenerator_targetPackage", DEFAULT_PACKAGE+".mapper."+entry.getKey());
			context.put("generator_sqlMapGenerator_targetPackage", "mapper."+entry.getKey());
			context.put("targetProject", bashPath+"/"+project);
			context.put("targetProject_sqlMap", bashPath+"/"+project);
			context.put("last_insert_id_tables", LAST_INSERT_ID_TABLES);
			VelocityUtil.generate(GENERATOR_CONFIG_VM, GENERATOR_CONFIG_XML, context);
			List<String> warnings = new ArrayList<String>();
			File configFile = new File(GENERATOR_CONFIG_XML);
			ConfigurationParser cp = new ConfigurationParser(warnings);
			Configuration config = cp.parseConfiguration(configFile);
			DefaultShellCallback callback = new DefaultShellCallback(true);
			MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
			myBatisGenerator.generate(null);

			String controllerPath = bashPath + "/src/main/java/" + DEFAULT_PACKAGE.replaceAll("\\.", "/");
			String path = bashPath + "/src/main/java/" + DEFAULT_PACKAGE.replaceAll("\\.", "/");
			String daoPath = bashPath + "/src/main/java/" + DEFAULT_PACKAGE.replaceAll("\\.", "/");
			for (int i = 0; i < tableList.size(); i++) {
				String table = StrUtils.lineToHump(ObjectUtils.toString(tableList.get(i).get("table_name")));
				generatorController(table, controllerPath, entry.getKey());
				generatorService(table, path, entry.getKey());
				generatorDao(table, daoPath, entry.getKey());
			}
		 }
	}

	/**
	 * 生成Controller
	 * @param table
	 * @param module
	 * @throws Exception
	 */
	public static void generatorController(String table, String path, String module) throws Exception {
		String controllerSrc =  path+ "/api/"+module+"/"+table+"Controller.java";
		//生成服务接口实现文件
		VelocityContext controllerContext = new VelocityContext();
		controllerContext.put("package_name", DEFAULT_PACKAGE);
		controllerContext.put("module", module);
		controllerContext.put("table", table);
		controllerContext.put("attr", StrUtils.firstLowerCase(table));
		VelocityUtil.generate(CONTROLLER_VM, controllerSrc, controllerContext);
	}

	/**
	 * 生成Service
	 * @param table
	 * @param module
	 * @throws Exception
	 */
	public static void generatorService(String table, String path, String module) throws Exception {
		String serviceSrc =  path+ "/service/"+module+"/"+table+"Service.java";
		String serviceImplSrc =  path+"/service/"+module+"/impl/"+table+"ServiceImpl.java";
		//生成服务接口文件 
		File serviceFile = new File(serviceSrc);
		if(!serviceFile.exists()) {
			VelocityContext serviceContext = new VelocityContext();
			serviceContext.put("package_name", DEFAULT_PACKAGE);
			serviceContext.put("table", table);
			serviceContext.put("module", module);
			serviceContext.put("attr", StrUtils.toLowerCaseFirstOne(table));
			VelocityUtil.generate(SERVICE_VM, serviceSrc, serviceContext);
		}
		
		//生成服务接口实现文件 
		File serviceImplFile = new File(serviceImplSrc);
		if(!serviceImplFile.exists()) {
			VelocityContext serviceContext = new VelocityContext();
			serviceContext.put("package_name", DEFAULT_PACKAGE);
			serviceContext.put("module", module);
			serviceContext.put("table", table);
			serviceContext.put("attr", StrUtils.toLowerCaseFirstOne(table));
			VelocityUtil.generate(SERVICE_IMPL_VM, serviceImplSrc, serviceContext);
		}
	}
	
	public static void generatorDao(String table, String path, String module) throws Exception {
		String serviceSrc =  path+ "/dao/"+module+"/"+table+"Dao.java";
		String serviceImplSrc =  path+"/dao/"+module+"/impl/"+table+"DaoImpl.java";
		//生成服务接口文件 
		File serviceFile = new File(serviceSrc);
		if(!serviceFile.exists()) {
			VelocityContext serviceContext = new VelocityContext();
			serviceContext.put("package_name", DEFAULT_PACKAGE);
			serviceContext.put("table", table);
			serviceContext.put("module", module);
			serviceContext.put("attr", StrUtils.toLowerCaseFirstOne(table));
			VelocityUtil.generate(DAO_VM, serviceSrc, serviceContext);
		}
		
		//生成服务接口实现文件 
		File serviceImplFile = new File(serviceImplSrc);
		if(!serviceImplFile.exists()) {
			VelocityContext serviceContext = new VelocityContext();
			serviceContext.put("package_name", DEFAULT_PACKAGE);
			serviceContext.put("module", module);
			serviceContext.put("table", table);
			serviceContext.put("attr", StrUtils.toLowerCaseFirstOne(table));
			VelocityUtil.generate(DAO_IMPL_VM, serviceImplSrc, serviceContext);
		}
	}
	
	public void generatorController() {
		
	}
}
